package db

import (
	"database/sql"
	"fmt"
	"logbeat/internal/globalvar"

	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

func CheckDB() *sql.DB {
	dbPath := globalvar.SysConf.DBPath
	if dbPath == "" {
		dbPath = "./logbeat.db"
	}
	db, err := gorm.Open(sqlite.Open(globalvar.SysConf.DBPath), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}
	globalvar.DB = db

	SqlDB, err := globalvar.DB.DB()
	if err != nil {
		panic("failed to connect database")
	}

	// 自动判断是否存在表，如果不存在则创建
	globalvar.DB.AutoMigrate(&LogFileRecord{})
	fmt.Println("checkDB", globalvar.DB)
	return SqlDB
}

type LogFileRecord struct {
	gorm.Model
	FileName string
	Offset   int
}
